﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ExitGames.Client.Photon;
using Common;
public class PhotonEngine : MonoBehaviour,IPhotonPeerListener
{
    public static PhotonPeer _Peer { get { return peer; } }
    public static PhotonEngine _Instance;
    private static PhotonPeer peer;
    private Dictionary<OperationCode, Request> RequestDict = new Dictionary<OperationCode, Request>();
    public static string userName;
    public void DebugReturn(DebugLevel level, string message)
    {
       // throw new System.NotImplementedException();
    }

    /// <summary>
    /// 服务器向客户端发送的事件
    /// </summary>
    /// <param name="eventData"></param>
    public void OnEvent(EventData eventData)
    {
        switch (eventData.Code)
        {
            case 0:
                break;
            case 1:
             
            Dictionary<byte,object> data=  eventData.Parameters;
                object v3;
                data.TryGetValue(1, out v3);
                object str;
                data.TryGetValue(2, out str);
                print("收到服务器端的一条事件"+v3+"----"+str);
                break;
            default:
                break;
        }


    }

    /// <summary>
    /// 请求之后，回应时候调用
    /// </summary>
    /// <param name="operationResponse"></param>
    public void OnOperationResponse(OperationResponse operationResponse)
    {
        OperationCode opCode = (OperationCode)operationResponse.OperationCode;
        Request request = null;
      bool temp=  RequestDict.TryGetValue(opCode, out request);
        if (temp)
        {
            request.OnPerarionResponse(operationResponse);
        }
        else 
        {
            Debug.Log("没有找到操作符号OperationCode"+ opCode);
        }

        //switch (operationResponse.OperationCode)
        //{
        //    case 0:
        //        break;
        //    case 1:
        //  Dictionary<byte,object> data=      operationResponse.Parameters;

        //        object v3;
        //        data.TryGetValue(1, out v3);
        //        object str;
        //        data.TryGetValue(2, out str);

        //        print("服务器传回一条数据"+v3+"-----------"+str.ToString());
        //        break;
        //    default:
        //        break;
        //}
    }

    public void OnStatusChanged(StatusCode statusCode)
    {

        Debug.Log(statusCode.ToString());
    }

    private void Awake()
    {
        if (_Instance == null)
        {
            _Instance = this;
            DontDestroyOnLoad(this.gameObject);
        }
        else if(_Instance!=this)
        {
            Destroy(this.gameObject);
            return;
        }
    }

    // Start is called before the first frame update
    void Start()
    {
        //通过Listender接受服务端的相应
        peer = new PhotonPeer(this,ConnectionProtocol.Udp);
        peer.Connect("127.0.0.1:5055","MyGame1");
       // peer.OpCustom();
    }

    // Update is called once per frame
    void Update()
    {
        peer.Service();
    }

    void OnDestroy() 
    {
        if (peer!=null&&peer.PeerState==PeerStateValue.Connected) 
        {
            peer.Disconnect();
        }
    }

 public    void AddRequest(Request request) 
    {
        RequestDict.Add(request.OpCode, request);
    }


    public void RemoveRequest(Request request)
    {
        RequestDict.Remove(request.OpCode);
    }
}
